var defaultfare;
var faretemps;
$(function () {
    try {
        var showproduct = {
            "boxid": "showbox",
            "sumid": "showsum",
            "boxw": 380,//大图width
            "boxh": 380,//大图height
            "sumw": 58,//小图width
            "sumh": 58,//小图height
            "sumi": 5,//小图margint-left
            "sums": 5,//小图列表数
            "sumsel": "sel",
            "sumborder": 1,//列表边框，没有边框填写0，边框在css中修改
            "lastid": "showlast",
            "nextid": "shownext"
        };//参数定义
        $.ljsGlasses.pcGlasses(showproduct);//方法调用，务必在加载完后执行
        $("#J_tab_info .tab-con-nav li").tab({tabBd: "#J_tab_info .tab-con-bd-item", _event: "click"});
    } catch (w) {
    }

    setBrowsepriceid($("[name='priceid']").val(), $("[name='clscode']").val());
    $("#maylikemore").attr("href", $("#maylikemore").attr("href") + "?clscode=" + getBrowsclscode());

    loadMaylike(6, loadMaylikeok);

});

//计算运费
function computefare(p, c, amount) {
    var faretemp;
    for (var i = 0; i < faretemps.length; i++) {
        if (faretemps[i].province.indexOf(p) != -1 && faretemps[i].city.indexOf(c) != -1) {
            faretemp = faretemps[i];
            break;
        }
    }
    if (!faretemp) faretemp = defaultfare;
    var fare = 0;
    if (faretemp) {
        if (amount <= faretemp.startnum) fare = faretemp.startmoney;
        else {
            var pix = (faretemp.nextmoney / faretemp.nextnum).tofixed(2);
            var diff = amount - faretemp.startnum;//续件
            fare = (diff * pix).tofixed(2);
        }
    }
    $("#fare").text("￥" + fare);
}

function addamount() {
    var txt = $("[name='amount']");
    var n = parseInt(txt.val());
    n++;
    txt.val(n);
    inputAmount(txt);
}
function lessenamount() {
    var txt = $("[name='amount']");
    var n = parseInt(txt.val());
    n--;
    txt.val(n);
    inputAmount(txt);
}
function addtocart() {
    var url = ctx + "/order/addToCart?pid=" + $("[name='priceid']").val() + "&amount=" + $("[name='amount']").val();
    //$w.checkLogin(true,url);
    window.open(url);
}

function toggleOften(obj) {
    var flag = 1;
    if ($(".product-detail-bottom .collect .join-list").hasClass("gray")) {
        flag = 0;
    }
    if (flag) {
        delFavorite(obj);
    } else {
        saveFavorite(obj);
    }
}

function saveFavorite(obj) {
    $w.httpRequest({
        url: apiroot + '/product/favorite',
        data: {"priceid": $("[name='priceid']").val(), "action": 0},
        ok: function (ret) {
            new auiToast().success({
                title: "收藏成功",
                duration: 300
            });
            $(obj).find("span").text("已收藏");
            $(obj).addClass("ico-keep-blue");
            $(".product-detail-bottom .collect .join-list").removeClass("gray");
        },
        fail: function (ret) {
            if (ret.MsgCode) {
                new auiToast().fail({
                    title: ret.MsgDesc,
                    duration: 300
                });
            } else {
                new auiToast().fail({
                    title: "收藏失败",
                    duration: 300
                });
            }

        }
    })
}
function delFavorite(obj) {
    $w.httpRequest({
        url: apiroot + '/product/favorite',
        data: {"priceid": $("[name='priceid']").val(), "action": 1},
        ok: function (ret) {
            new auiToast().success({
                title: "取消收藏成功",
                duration: 300
            });
            $(obj).find("span").html("收藏");
            $(obj).removeClass("ico-keep-blue");
            $(".product-detail-bottom .collect .join-list").addClass("gray");
        },
        fail: function (ret) {
            if (ret.MsgCode) {
                new auiToast().fail({
                    title: ret.MsgDesc,
                    duration: 300
                });
            } else {
                new auiToast().fail({
                    title: "取消收藏失败",
                    duration: 300
                });
            }
        }
    })
}

function saveOften(obj) {
    $w.httpRequest({
        url: apiroot + "/order/addOften",
        data: {"priceid": $("[name='priceid']").val()},
        ok: function (ret) {
            new auiToast().success({
                title: "加入常购清单成功",
                duration: 300
            });
            $(obj).find("span").text("已加入常购清单");
            $(obj).addClass("ico-keep-blue");
            $(".product-detail-bottom .collect .join-list").removeClass("gray");
        },
        fail: function (ret) {
            if (ret.MsgCode) {
                new auiToast().fail({
                    title: ret.MsgDesc,
                    duration: 300
                });
            } else {
                new auiToast().fail({
                    title: "加入常购清单失败",
                    duration: 300
                });
            }

        }
    })
}
function delOften(obj) {
    $w.httpRequest({
        url: apiroot + "/order/delOften",
        data: {"priceid": $("[name='priceid']").val()},
        ok: function (ret) {
            new auiToast().success({
                title: "取消常购清单成功",
                duration: 300
            });
            $(obj).find("span").html("加常购清单");
            $(obj).removeClass("ico-keep-blue");
            $(".product-detail-bottom .collect .join-list").addClass("gray");
        },
        fail: function (ret) {
            if (ret.MsgCode) {
                new auiToast().fail({
                    title: ret.MsgDesc,
                    duration: 300
                });
            } else {
                new auiToast().fail({
                    title: "取消常购清单失败",
                    duration: 300
                });
            }
        }
    })
}

function loadMaylikeok(datas) {
    var panel = $("#maylike");
    var size = datas.list.length;
    if (size % 2 == 0) {
        panel.addClass("double-bottom-shape");
    }
    for (var i = 0; i < size; i++) {
        var data = datas.list[i];
        var h = '<li><a href="' + ctx + '/we/product/' + data.id + '">'
            + '<img src="' + fileserver + data.photo + '" onerror="this.src=\'' + ctx + '/website/images/noimg.jpg\'"/></a>'
            + '<h3>' + data.productname + ' ' + data.name + ' ' + (data.b_guige || '') + '</h3>'
            + '<span class="company">' + data.b_chanjia + '</span>';
        if (isuserauth && islogin) h += '<span class="price"><i class="a">￥' + data.price + '</i></span>';
        else if (!islogin) h += '<span class="price"><i class="a">登录可见</i></span>';
        else h += '<span class="price"><i class="a">审核通过可见</i></span>';
        h += '</li>';
        panel.append(h);
    }
}

function productfavorite(obj) {
    if ($w.checkLogin(true, ctx + "/product/info?priceid=" + $("[name='priceid']").val())) {
        if ($("i", $(obj)).get(0).className.indexOf("fa-star-o") != -1) {
            $w.httpRequest({
                url: apiroot + '/product/favorite',
                data: {priceid: $("[name='priceid']").val(), "action": 0},
                ok: function (ret) {
                    $(obj).html('<i class="fa fa-star-o-me2"></i>商品已收藏')
                }
            })
        } else {
            $w.httpRequest({
                url: apiroot + '/product/favorite',
                data: {priceid: $("[name='priceid']").val(), "action": 1},
                ok: function (ret) {
                    $(obj).html('<i class="fa fa-star-o-me"></i>收藏商品');
                }
            })
        }
    }
}

function addtongz(obj) {
    $(obj).text("已添加到货提醒")
}